using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class CampaignPrepare : MonoBehaviour
{
	public GameObject itemListView;

	public GameObject itemViewTemplate;

	public GameObject itemEmptyViewTemplate;

	public GameObject itemListCanvas;

	public Text dayTextView;

	public GameObject dayCanvasGameObject;

	[HideInInspector]
	public GameObject onSelectItemView;

	public GameObject primaryWeaponItemView;

	public GameObject secondaryWeaponItemView;

	public GameObject meleeWeaponItemView;

	public GameObject suitItemView;

	public GameObject petItemView;

	public Alert alert;

	public static bool gameStart;

	public Texture2D cursorTexture;

	public Texture2D cursorPressTexture;

	public Transform tips;

	public Transform dayTips;


	public List<string> unavailableItems = new List<string>();

	private void Awake()
	{
		if (Config.initialized)
		{
			new Config().Init();
		}
	}

	private void Start()
	{
		int childCount = tips.transform.childCount;
		if (DO.Campaign.day - 1 < childCount)
		{
			dayTips = tips.transform.GetChild(DO.Campaign.day - 1);
		}
		else
		{
			dayTips = tips.transform.GetChild(childCount - 1);
		}
		dayTips.gameObject.SetActive(value: true);
		foreach (StageItem stageItem in DO.Campaign.stageItems)
		{
			if (stageItem.usedItemName != "")
			{
				unavailableItems.Add(stageItem.usedItemName);
			}
		}
		foreach (string availableItem in DO.Campaign.availableItems)
		{
			if (!(availableItem == "") && !unavailableItems.Contains(availableItem) && (bool)Resources.Load("Items/" + availableItem))
			{
				GameObject gameObject = Object.Instantiate(itemViewTemplate, itemListView.transform);
				gameObject.name = availableItem;
				GameObject gameObject2 = gameObject.transform.Find("Pic").gameObject;
				Text component = gameObject.transform.Find("Name").gameObject.GetComponent<Text>();
				Item item = AddItemPic(availableItem, gameObject2.transform);
				if (DO.Campaign.pet == "Init" && item.type == ItemType.Pet)
				{
					DO.Campaign.pet = item.name.Replace("(Clone)", "");
				}
				if ((bool)item)
				{
					component.text = item.Title;
				}
				else
				{
					component.text = LocalizationManager.GetTranslation(gameObject.name);
				}
			}
		}
		foreach (Transform item2 in itemListView.transform)
		{
			Button btn = item2.transform.Find("Button").gameObject.GetComponent<Button>();
			btn.onClick.AddListener(delegate
			{
				Equip(btn.transform.parent.gameObject);
			});
		}
		UpdateItemView(DO.Campaign.primaryWeapon, primaryWeaponItemView);
		UpdateItemView(DO.Campaign.secondaryWeapon, secondaryWeaponItemView);
		UpdateItemView(DO.Campaign.meleeWeapon, meleeWeaponItemView);
		UpdateItemView(DO.Campaign.suit, suitItemView);
		if (DO.Campaign.pet != "Init")
		{
			UpdateItemView(DO.Campaign.pet, petItemView);
		}
		dayCanvasGameObject.SetActive(value: false);
	}

	private void Update()
	{
	}

	private IEnumerator ShowBannerWhenInitialized()
	{
		while (!Advertisement.isInitialized)
		{
			yield return new WaitForSeconds(0.5f);
		}
	}

	public void CloseItemList()
	{
		itemListCanvas.GetComponent<Canvas>().planeDistance = 0f;
	}

	public void Unequip()
	{
		if (onSelectItemView == primaryWeaponItemView)
		{
			DO.Campaign.primaryWeapon = null;
		}
		else if (onSelectItemView == secondaryWeaponItemView)
		{
			DO.Campaign.secondaryWeapon = null;
		}
		else if (onSelectItemView == meleeWeaponItemView)
		{
			DO.Campaign.meleeWeapon = null;
		}
		else
		{
			if (onSelectItemView == suitItemView)
			{
				alert.Show(LocalizationManager.GetTranslation("UnequipSuitAlert"));
				return;
			}
			if (onSelectItemView == petItemView)
			{
				DO.Campaign.pet = null;
			}
		}
		UpdateItemView(DO.Campaign.primaryWeapon, primaryWeaponItemView);
		UpdateItemView(DO.Campaign.secondaryWeapon, secondaryWeaponItemView);
		UpdateItemView(DO.Campaign.meleeWeapon, meleeWeaponItemView);
		UpdateItemView(DO.Campaign.suit, suitItemView);
		UpdateItemView(DO.Campaign.pet, petItemView);
		CloseItemList();
	}

	public void Equip(GameObject item)
	{
		if (onSelectItemView == primaryWeaponItemView)
		{
			DO.Campaign.primaryWeapon = item.name;
			if (item.name == DO.Campaign.secondaryWeapon)
			{
				DO.Campaign.secondaryWeapon = null;
			}
		}
		else if (onSelectItemView == secondaryWeaponItemView)
		{
			DO.Campaign.secondaryWeapon = item.name;
			if (item.name == DO.Campaign.primaryWeapon)
			{
				DO.Campaign.primaryWeapon = null;
			}
		}
		else if (onSelectItemView == meleeWeaponItemView)
		{
			DO.Campaign.meleeWeapon = item.name;
		}
		else if (onSelectItemView == suitItemView)
		{
			DO.Campaign.suit = item.name;
		}
		else if (onSelectItemView == petItemView)
		{
			DO.Campaign.pet = item.name;
		}
		UpdateItemView(DO.Campaign.primaryWeapon, primaryWeaponItemView);
		UpdateItemView(DO.Campaign.secondaryWeapon, secondaryWeaponItemView);
		UpdateItemView(DO.Campaign.meleeWeapon, meleeWeaponItemView);
		UpdateItemView(DO.Campaign.suit, suitItemView);
		if (DO.Campaign.pet != "Init")
		{
			UpdateItemView(DO.Campaign.pet, petItemView);
		}
		CloseItemList();
	}

	public void UpdateItemView(string itemName, GameObject itemView)
	{
		Text component = itemView.transform.Find("Name").GetComponent<Text>();
		Transform transform = itemView.transform.Find("Pic");
		Item item = AddItemPic(itemName, transform);
		if ((bool)item)
		{
			component.text = item.Title;
		}
		else
		{
			component.text = LocalizationManager.GetTranslation(itemView.name);
		}
	}

	public Item AddItemPic(string itemName, Transform transform)
	{
		if (transform.childCount > 0)
		{
			Object.Destroy(transform.GetChild(0).gameObject);
		}
		switch (itemName)
		{
			case "":
			case null:
			case "Init":
				return null;
			default:
				{
					Item component = Object.Instantiate((GameObject)Resources.Load("Items/" + itemName), transform).GetComponent<Item>();
					Vector2 vector = new Vector2(60f, 60f);
					component.transform.localScale = vector;
					return component;
				}
		}
	}

	public void OpenItemList(GameObject itemView)
	{
		if (itemView.name == "PrimaryWeapon" || itemView.name == "SecondaryWeapon")
		{
			foreach (Transform item in itemListView.transform)
			{
				item.gameObject.SetActive(value: true);
				item.Find("Pic");
				if (item.Find("Pic").GetChild(0).GetComponent<Item>()
					.type != 0)
				{
					item.gameObject.SetActive(value: false);
				}
			}
		}
		else if (itemView.name == "MeleeWeapon")
		{
			foreach (Transform item2 in itemListView.transform)
			{
				item2.gameObject.SetActive(value: true);
				if (item2.Find("Pic").GetChild(0).GetComponent<Item>()
					.type != ItemType.MeleeWeapon)
				{
					item2.gameObject.SetActive(value: false);
				}
			}
		}
		else if (itemView.name == "CombatSuit")
		{
			foreach (Transform item3 in itemListView.transform)
			{
				item3.gameObject.SetActive(value: true);
				if (item3.Find("Pic").GetChild(0).GetComponent<Item>()
					.type != ItemType.Suit)
				{
					item3.gameObject.SetActive(value: false);
				}
			}
		}
		else if (itemView.name == "Pet")
		{
			foreach (Transform item4 in itemListView.transform)
			{
				item4.gameObject.SetActive(value: true);
				if (item4.Find("Pic").GetChild(0).GetComponent<Item>()
					.type != ItemType.Pet)
				{
					item4.gameObject.SetActive(value: false);
				}
			}
		}
		onSelectItemView = itemView;
		itemListCanvas.GetComponent<Canvas>().planeDistance = 1f;
	}

	public void Finish()
	{
		if (DO.Campaign.primaryWeapon + DO.Campaign.secondaryWeapon + DO.Campaign.meleeWeapon == "")
		{
			alert.Show(LocalizationManager.GetTranslation("NoWeaponAlert"));
			return;
		}
		MyAdManager.Instance.MyShowNative();
		string content = JsonUtility.ToJson(DO.Campaign);
		SaveLoad.SaveString("Campaign", content);
		dayCanvasGameObject.SetActive(value: true);
		GameStart();
	}

	private void FixedUpdate()
	{
	}

	public void GameStart()
	{
		SceneManager.LoadScene("CampaignGame");
	}

	public IEnumerator LoadAsyncScene()
	{
		yield return null;
		float minFinishTime = Time.time;
		if (dayTips.name != "Null")
		{
			minFinishTime += 2f;
		}
		AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("CampaignGame");
		asyncOperation.allowSceneActivation = false;
		while (!asyncOperation.isDone)
		{
			if (asyncOperation.progress >= 0.9f && Time.time > minFinishTime)
			{
				asyncOperation.allowSceneActivation = true;
			}
			yield return null;
		}
	}
}
